/*
 * @lc app=leetcode.cn id=10 lang=java
 *
 * [10] 正则表达式匹配
 *
 * https://leetcode-cn.com/problems/regular-expression-matching/description/
 *
 * algorithms
 * Hard (23.04%)
 * Likes:    485
 * Dislikes: 0
 * Total Accepted:    17.1K
 * Total Submissions: 74K
 * Testcase Example:  '"aa"\n"a"'
 *
 * 给你一个字符串 s 和一个字符规律 p，请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。
 * 
 * '.' 匹配任意单个字符
 * '*' 匹配零个或多个前面的那一个元素
 * 
 * 
 * 所谓匹配，是要涵盖 整个 字符串 s的，而不是部分字符串。
 * 
 * 说明:
 * 
 * 
 * s 可能为空，且只包含从 a-z 的小写字母。
 * p 可能为空，且只包含从 a-z 的小写字母，以及字符 . 和 *。
 * 
 * 
 * 示例 1:
 * 
 * 输入:
 * s = "aa"
 * p = "a"
 * 输出: false
 * 解释: "a" 无法匹配 "aa" 整个字符串。
 * 
 * 
 * 示例 2:
 * 
 * 输入:
 * s = "aa"
 * p = "a*"
 * 输出: true
 * 解释: 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此，字符串 "aa" 可被视为 'a' 重复了一次。
 * 
 * 
 * 示例 3:
 * 
 * 输入:
 * s = "ab"
 * p = ".*"
 * 输出: true
 * 解释: ".*" 表示可匹配零个或多个（'*'）任意字符（'.'）。
 * 
 * 
 * 示例 4:
 * 
 * 输入:
 * s = "aab"
 * p = "c*a*b"
 * 输出: true
 * 解释: 因为 '*' 表示零个或多个，这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。
 * 
 * 
 * 示例 5:
 * 
 * 输入:
 * s = "mississippi"
 * p = "mis*is*p*."
 * 输出: false
 * 
 */
class Solution {
    public boolean isMatch(String s, String p) {
        final RuleBook ruleBook = parse(p);
        // ruleBook.rules.forEach(System.out::println);

        Set<Integer> states = new HashSet<>();
        states.add(0);

        for (char c : s.toCharArray()) {
            states = ruleBook.nextStates(states, c);
            // System.out.println(states);
        }

        for (Integer state : states) {
            if (ruleBook.applies.contains(state)) {
                return true;
            }
        }

        return false;
    }
    public RuleBook parse(String p) {
        p = p + "$";

        final RuleBook ruleBook = new RuleBook();
        int curState = 0;
        for (int i = 0; i < p.length(); i++) {
            final char c = p.charAt(i);

            if (c == '*') {
                continue;
            }

            int nextState = curState + 1;
            ruleBook.addRule(new Rule(curState, c, nextState++));

            boolean hasStar = false;
            for (int j = i + 1; j < p.length(); j++) {
                final char next = p.charAt(j);
                if (next == '*') {
                    if (j == i + 1) {
                        ruleBook.addRule(new Rule(curState + 1, c, curState + 1));
                    }
                    hasStar = true;
                } else {
                    if (hasStar) {
                        ruleBook.addRule(new Rule(curState, next, nextState++));
                        hasStar = false;
                    } else {
                        break;
                    }

                    if (j == p.length() - 1) {//最后一个了
                        break;
                    }

                    if (p.charAt(j + 1) != '*') {
                        break;
                    }
                }
            }
            curState++;
        }
    //    ruleBook.rules.forEach(System.out::println);
        return ruleBook;
    }


    class RuleBook {
        List<Rule> rules = new ArrayList<>();
        Set<Integer> applies = new HashSet<>();

        RuleBook addRule(Rule rule) {
            this.rules.add(rule);
            if (rule.input == '$') {
                applies.add(rule.curState);
            }
            return this;
        }

        Set<Integer> nextStates(Set<Integer> states, char c) {
            return rules.stream().filter(rule -> states.contains(rule.curState) && (rule.input == c || rule.input == '.')).map(rule -> rule.nextState).collect(Collectors.toSet());
        }
    }

    class Rule {
        int curState;
        char input;
        int nextState;

        public Rule(int curState, char input, int nextState) {
            this.curState = curState;
            this.input = input;
            this.nextState = nextState;
        }

        @Override
        public String toString() {
            return "Rule{" +
                    "curState=" + curState +
                    ", input=" + input +
                    ", nextState=" + nextState +
                    '}';
        }
    }
}

